home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / Erics C++ Libraries / Interface Classes / CPPControl.cp < prev    next >
Encoding:
Text File  |  1996-04-04  |  6.5 KB  |  256 lines  |  [TEXT/KAHL]

  1. /********************************************************* DEFINITION
  2.     DATE:    9/23/93
  3.     AUTHOR: Eric R. Rosé
  4.     
  5.     CLASS:  CPPControl
  6.     
  7.     SUPERCLASS: CPPVisualObject
  8.     
  9.         This C++ class manages a Button control
  10.     
  11. ********************************************************************/
  12.  
  13. #include <CPPControl.h>
  14. #include <CPPWindow.h>
  15.  
  16. extern Rect kDefaultRect;
  17. extern Rect kEmptyRect;
  18.  
  19. /*-----------------------------------------------------------------*/
  20. /*------------------------ PUBLIC METHODS -------------------------*/
  21. /*-----------------------------------------------------------------*/
  22.  
  23.     CPPControl::CPPControl (CPPWindow *itsWindow, short ResID,
  24.                               Boolean isFramed,
  25.                              Boolean canBeTarget,
  26.                              Boolean active, Boolean visible) :
  27.                 CPPVisualObject (itsWindow, &kDefaultRect, canBeTarget, 
  28.                                  active, visible)
  29.     /* load a control resource and initialize with the given data */
  30.     {
  31.         
  32.         if (this->owningWindow)
  33.           {
  34.             this->theControl = GetNewControl (ResID, this->owningWindow);
  35.             this->hasFrame = isFramed;
  36.             this->isEnabled = TRUE;
  37.             this->myValue = 0;
  38.           }
  39.         this->callBackProc = NULL;
  40.     }
  41.  
  42. /*-----------------------------------------------------------------*/
  43.  
  44.     CPPControl::CPPControl (CPPWindow *itsWindow, Rect *itsBounds,
  45.                              short ControlType, StringPtr itsText,
  46.                               Boolean isFramed,
  47.                               Boolean useWindowFont,
  48.                              Boolean canBeTarget,
  49.                              Boolean active, Boolean visible) :
  50.                 CPPVisualObject (itsWindow, itsBounds, canBeTarget, 
  51.                                  active, visible)
  52.     {
  53.         short    procid = (useWindowFont) ? ControlType | 8 : ControlType;
  54.         
  55.         if (this->owningWindow)
  56.           this->theControl = 
  57.                   NewControl(this->owningWindow, itsBounds, itsText,
  58.                               visible, 0, 0, 1, procid, 13);
  59.         this->hasFrame = isFramed;
  60.         this->isEnabled = TRUE;
  61.         this->myValue = 0;
  62.         this->callBackProc = NULL;
  63.     }
  64.  
  65. /*-----------------------------------------------------------------*/
  66.  
  67.     CPPControl::~CPPControl (void)
  68.     {
  69.         if (this->theControl)
  70.           DisposeControl(this->theControl);
  71.     }
  72.  
  73. /*-----------------------------------------------------------------*/
  74.  
  75.     char    *CPPControl::ClassName (void)
  76.     {
  77.         return "CPPControl";
  78.     }
  79.  
  80. /*-----------------------------------------------------------------*/
  81.  
  82.     Boolean    CPPControl::DoClick (EventRecord *theEvent)
  83.     /* handle a click in the control - if visible */
  84.     {
  85.         Point    myPt = theEvent->where;
  86.         
  87.         if (this->theControl && this->IsVisible() && this->isEnabled)
  88.           {
  89.               Global2Local (&myPt);
  90.                if (TrackControl(this->theControl, myPt, this->callBackProc))
  91.                  DoOnClick ();
  92.                return TRUE;
  93.           }
  94.         
  95.         return FALSE;
  96.     }
  97.  
  98. /*-----------------------------------------------------------------*/
  99.  
  100.     void    CPPControl::Activate (Boolean nowActive)
  101.     /* activate/deactivate our control */
  102.     /* SUBCLASS SHOULD OVERRIDE */
  103.     {
  104.         // controls do not usually change their appearance
  105.         // when in background or foreground
  106.         
  107.         CPPVisualObject::Activate(nowActive);
  108.     }
  109.  
  110. /*-----------------------------------------------------------------*/
  111.  
  112.     void    CPPControl::MakeVisible (Boolean nowVisible)
  113.     /* hide/show our control */
  114.     /* IS OVERRIDE */
  115.     {
  116.         if (this->theControl)
  117.           {
  118.               if (nowVisible)
  119.                 ShowControl (this->theControl);
  120.               else
  121.                 HideControl (this->theControl);
  122.           }
  123.           
  124.         CPPVisualObject::MakeVisible(nowVisible);
  125.     }
  126.     
  127. /*-----------------------------------------------------------------*/
  128.  
  129.     void    CPPControl::Draw ()
  130.     /* is override */
  131.     {
  132.         PenState    OldState;
  133.         Rect        frameRect = *GetBounds();
  134.         
  135.         if (this->IsVisible() && this->theControl)
  136.           {
  137.               Draw1Control (theControl);
  138.               if (this->hasFrame)
  139.                 FrameControl(GetCVariant(this->theControl) == pushButProc);
  140.           }
  141.     }
  142.  
  143. /*-----------------------------------------------------------------*/
  144.  
  145.     void    CPPControl::FrameControl (Boolean useRoundRect)
  146.     /* draw the standard 3-pixel wide border around the control */
  147.     {
  148.         PenState    OldState;
  149.         Rect        frameRect = *GetBounds();
  150.             
  151.             GetPenState(&OldState);
  152.             InsetRect(&frameRect, -4, -4);
  153.             PenPat(black);
  154.             PenSize (3,3);
  155.             if (useRoundRect)
  156.               FrameRoundRect(&frameRect, 16, 16);
  157.             else
  158.               FrameRect (&frameRect);
  159.             SetPenState(&OldState);
  160.     }
  161.  
  162. /*-----------------------------------------------------------------*/
  163.  
  164.     void    CPPControl::SetValue (short newValue)
  165.     /* set the control's value to the value passed to us */
  166.     /* SUBCLASS MAY OVERRIDE */
  167.     {
  168.         SetCtlValue (this->theControl, this->myValue = newValue);
  169.     }
  170.     
  171. /*-----------------------------------------------------------------*/
  172.  
  173.     short    CPPControl::GetValue (void)
  174.     /* return the value of the control */
  175.     {
  176.         return this->myValue;
  177.     }
  178.  
  179. /*-----------------------------------------------------------------*/
  180.  
  181.     void    CPPControl::SetCallBack (ProcPtr callBack)
  182.     {
  183.         this->callBackProc = callBack;
  184.     }
  185.  
  186. /*-----------------------------------------------------------------*/
  187.  
  188.     Boolean    CPPControl::IsEnabled (void)
  189.     {
  190.         return this->isEnabled;
  191.     }
  192.     
  193. /*-----------------------------------------------------------------*/
  194.  
  195.     void    CPPControl::DoOnClick (void)
  196.     /* perform some action when the control is clicked */
  197.     /* SUBCLASS SHOULD OVERRIDE */
  198.     {
  199.         if (this->doClickProc)    // call the user-specified click handler
  200.          (doClickProc)(this->owningWObject);
  201.         else
  202.         if (this->commandEquivalent != kNoCommand)
  203.           this->owningWObject->DoCommand (this->commandEquivalent);
  204.     }
  205.  
  206. /*-----------------------------------------------------------------*/
  207.  
  208.     void    CPPControl::EnableControl (Boolean nowEnabled)
  209.     {
  210.         if (this->theControl)
  211.           {
  212.               if (nowEnabled != this->isEnabled)
  213.                 {
  214.                   InvalRect (GetBounds());
  215.                   this->isEnabled = nowEnabled;
  216.                   HiliteControl (this->theControl, (nowEnabled) ? 0 : 255);
  217.                 }
  218.           }
  219.     }
  220.  
  221. /*-----------------------------------------------------------------*/
  222.  
  223.     Rect    *CPPControl::GetBounds (void)
  224.     /* return the boundsrect of the object */
  225.     /* SUBCLASS MUST OVERRIDE */
  226.     {
  227.         if (this->theControl && this->IsVisible())
  228.           this->bounds = (*this->theControl)->contrlRect;
  229.         else
  230.           this->bounds = kEmptyRect;
  231.         
  232.         return &this->bounds;
  233.     }
  234.  
  235. /*-----------------------------------------------------------------*/
  236. /*---------------------- PROTECTED METHODS ------------------------*/
  237. /*-----------------------------------------------------------------*/
  238.  
  239.     void    CPPControl::MoveContent (short newH, short newV)
  240.     /* move the entire button to a new position */
  241.     {
  242.         if (this->theControl)
  243.           MoveControl (this->theControl, newH, newV);
  244.     }
  245.  
  246. /*-----------------------------------------------------------------*/
  247.  
  248.     void    CPPControl::ResizeContent (short newWidth, short newHeight)
  249.     /* change the size of the button */
  250.     {
  251.         if (this->theControl)
  252.           SizeControl(this->theControl, newWidth, newHeight);
  253.     }
  254.  
  255.  
  256.